package com.jinzhi.jzweb.controller.backstage;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.constants.ConstMessages;
import com.jinzhi.common.constants.MessagesEnum;
import com.jinzhi.common.utils.Result;
import com.jinzhi.common.utils.ShiroUtils;
import com.jinzhi.jzweb.domain.JazMessageDO;
import com.jinzhi.jzweb.domain.JazSysLabelDO;
import com.jinzhi.jzweb.domain.JazSysProjectDO;
import com.jinzhi.jzweb.domain.JazUserAuntDO;
import com.jinzhi.jzweb.domain.param.UserAuntQuery;
import com.jinzhi.jzweb.domain.vo.userAunt.UserAuntVO;
import com.jinzhi.jzweb.domain.vo.userAunt.UserVO;
import com.jinzhi.jzweb.service.*;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*
 *  @author : JingChangTian
 *  @date   : 2023/7/11 9:30
 *  @description :  月嫂后台管理
 *
 */
@Controller
@RequestMapping("/userAunt")
@Api(value = "月嫂后台管理", tags = "月嫂后台管理")
public class UserAuntController {

    /**
     * 标签
     */
    private final JazSysLabelService jazSysLabelService;

    /**
     * 项目类型
     */
    private final JazSysProjectService jazSysProjectService;

    /**
     * 月嫂信息
     */
    private final JazUserAuntService jazUserAuntService;

    private final JazMessageService jazMessageService;

    @Autowired
    private JazUserHouseService jazUserHouseService;
    public UserAuntController(JazSysLabelService jazSysLabelService, JazSysProjectService jazSysProjectService, JazUserAuntService jazUserAuntService, JazMessageService jazMessageService) {
        this.jazSysLabelService = jazSysLabelService;
        this.jazSysProjectService = jazSysProjectService;
        this.jazUserAuntService = jazUserAuntService;
        this.jazMessageService = jazMessageService;
    }


    @ApiOperation(value = "查询未禁用标签", notes = "查询未禁用标签")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @GetMapping("/seleLabel")
    public Result<List<JazSysLabelDO>> seleLabel(){
        List<JazSysLabelDO> jazSysLabelDOS = jazSysLabelService.selectList(new EntityWrapper<JazSysLabelDO>().eq("status",0));
        return Result.ok(jazSysLabelDOS);
    }

    @ApiOperation(value = "查询未禁用项目类型", notes = "查询未禁用项目类型")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @GetMapping("/seleProject")
    public Result<List<JazSysProjectDO>> seleProject(){
        List<JazSysProjectDO> jazSysProjectDOS = jazSysProjectService.selectList(new EntityWrapper<JazSysProjectDO>().eq("status",0));
        return Result.ok(jazSysProjectDOS);
    }

    @Log(value = "新增月嫂",sysModule = 2,operationType = 1)
    @ApiOperation(value = "新增月嫂信息", notes = "新增月嫂信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "UserAuntVO", value = "UserAuntVO", required = true, dataType = "UserAuntVO"),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @PostMapping("/insertAunt")
    public Result<String> insertAunt(@RequestBody UserAuntVO userAuntVO){
        return jazUserAuntService.insertAunt(userAuntVO);
    }


    @ApiOperation(value = "分页查月嫂信息", notes = "分页查月嫂信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "UserQuery", value = "UserQuery", required = true, dataType = "UserQuery"),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @PostMapping(value = "/getUserAuntPage")
    @RequiresPermissions("jzweb:jazUserAuntExamine:list")
    public Result<Page<JazUserAuntDO>> getUserAuntPage(@RequestBody UserAuntQuery userAuntQuery){
        return Result.ok(jazUserAuntService.getUserAuntPage(userAuntQuery));
    }

    @ApiOperation(value = "查月嫂信息", notes = "查月嫂信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "", value = "", required = true, dataType = ""),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @GetMapping(value = "/getUserAunt")
    public Result<List<UserVO>> getUserAunt(){
        return Result.ok(jazUserAuntService.getUserAunt());
    }

    @Log(value = "审核月嫂信息",sysModule = 2,operationType = 3)
    @ApiOperation(value = "审核月嫂信息", notes = "审核月嫂信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "JazUserAuntDO", value = "JazUserAuntDO", required = true, dataType = "JazUserAuntDO"),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @PostMapping(value = "/examineAunt")
    @Transactional(rollbackFor = Exception.class)
    public Result<String> examineAunt(@RequestBody JazUserAuntDO jazUserAuntDO){
        if (jazUserAuntDO==null){
            return Result.fail();
        }
        JazUserAuntDO auntDO = jazUserAuntService.selectById(jazUserAuntDO.getId());
        auntDO.setUpdateTime(new Date());
        auntDO.setAuditResult(jazUserAuntDO.getAuditResult());
        auntDO.setAuditStatus(jazUserAuntDO.getAuditStatus());
        auntDO.setRefuseReason(jazUserAuntDO.getRefuseReason());
        auntDO.setShowStatus(jazUserAuntDO.getShowStatus());
        Boolean  aBoolean = jazUserAuntService.updateById(jazUserAuntDO);
        // 发送审核结果消息
        if (jazUserAuntDO.getAuditResult()==0){
            // 通过
            JazMessageDO jazMessageDO1 = new JazMessageDO();
            jazMessageDO1.setMessageType(1);
            jazMessageDO1.setSenderId(ShiroUtils.getUserId());
            jazMessageDO1.setReceiverId(auntDO.getSysUserid());
            jazMessageDO1.setMessageContent(MessagesEnum.MESSAGE_102.getMsg());
            jazMessageDO1.setMessageNum(MessagesEnum.MESSAGE_102.getNum());
            jazMessageDO1.setAuntId(auntDO.getId());
            jazMessageDO1.setIsRead(0);
            jazMessageService.senMessage(jazMessageDO1);
        } else if (jazUserAuntDO.getAuditResult()==1) {
            // 拒绝
            JazMessageDO jazMessageDO1 = new JazMessageDO();
            jazMessageDO1.setMessageType(1);
            jazMessageDO1.setSenderId(ShiroUtils.getUserId());
            jazMessageDO1.setReceiverId(auntDO.getSysUserid());
            jazMessageDO1.setMessageContent(MessagesEnum.MESSAGE_103.getMsg()+"请上传"+auntDO.getRefuseReason());
            jazMessageDO1.setMessageNum(MessagesEnum.MESSAGE_103.getNum());
            jazMessageDO1.setAuntId(auntDO.getId());
            jazMessageDO1.setIsRead(0);
            jazMessageService.senMessage(jazMessageDO1);
        }
        return aBoolean ? Result.ok() : Result.fail();
    }


    @ApiOperation(value = "根据月嫂id查询详细信息", notes = "根据月嫂id查询详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long"),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @GetMapping(value = "/getUserAuntById")
    public Result<UserAuntVO> getUserAuntById(@RequestParam("id") Long id){
        return Result.ok(jazUserAuntService.getUserAuntById(id));
    }

    @Log(value = "修改月嫂信息",sysModule = 2,operationType = 3)
    @ApiOperation(value = "修改月嫂用户信息", notes = "修改月嫂用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "UserAuntVO", value = "UserAuntVO", required = true, dataType = "UserAuntVO"),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @PostMapping(value = "/updateUserAnut")
    public Result<UserAuntVO> updateUserAnut(@RequestBody UserAuntVO userAuntVO){
        return Result.ok(jazUserAuntService.updateUserAnut(userAuntVO));
    }

    @Log(value = "修改月嫂列表启用状态",sysModule = 2,operationType = 3)
    @ApiOperation(value = "修改月嫂列表启用状态", notes = "修改月嫂列表启用状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "UserAuntVO", value = "UserAuntVO", required = true, dataType = "UserAuntVO"),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @PostMapping(value = "/updateUserAnutStatus")
    public Result updateUserAnutStatus(@RequestBody JazUserAuntDO jazUserAuntDO){
        return jazUserAuntService.updateUserAnutStatus(jazUserAuntDO);
    }

    @ApiOperation(value = "月嫂审核列表页面入口", notes = "月嫂审核列表页面入口")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping("/jazUserAuntExamine")
    @RequiresPermissions("jzweb:jazUserAuntExamine:list")
    public String jazUserAuntExamine(){
        return "jzweb/jazUserAuntExamine/jazUserAuntExamine";
    }

    @ApiOperation(value = "月嫂住宿列表页面入口", notes = "月嫂住宿列表页面入口")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping("/jazUserAuntStay")
    @RequiresPermissions("jzweb:jazUserAuntStay:list")
    public String jazUserAuntStay(){
        return "jzweb/jazUserAuntStay/jazUserAuntStay";
    }

    @ApiOperation(value = "月嫂住宿列表页面入口", notes = "月嫂住宿列表页面入口")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping("/jazUserAuntStay/add")
    @RequiresPermissions("jzweb:jazUserAuntStay:add")
    public String jazUserAuntStayAdd(){
        return "jzweb/jazUserAuntStay/add";
    }

    @ApiOperation(value = "月嫂住宿列表页面入口", notes = "月嫂住宿列表页面入口")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping("/jazUserAuntStay/info/{id}")
    @RequiresPermissions("jzweb:jazUserAuntStay:info")
    public String jazUserAuntStayInfor(@PathVariable("id") Long id, Model model){
        model.addAttribute("myData", jazUserHouseService.getUserHouseById(id));
        return "jzweb/jazUserAuntStay/info";
    }


    @ApiOperation(value = "月嫂审核页面入口", notes = "月嫂审核页面入口")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping("/jazUserAuntExamineInfo")
    public String jazUserAuntExamineInfo(){
        return "jzweb/jazUserAuntExamine/jazUserAuntExamineInfo";
    }

    @ApiOperation(value = "月嫂详情页面入口", notes = "月嫂详情页面入口")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping("/info/{id}/{inforType}")
    @RequiresPermissions("jzweb:jazUserAunt:info")
    public String info(@PathVariable("id") Long id, @PathVariable("inforType") String inforType, Model model){
        //详情
        UserAuntVO userAuntVO=jazUserAuntService.getUserAuntById(id);
        //处理
       String experience=userAuntVO.getJazUserAuntDO().getExperience();
       String lifePhoto=userAuntVO.getJazUserAuntDO().getLifePhoto();
       String certificate=userAuntVO.getJazUserAuntDO().getCertificate();
       String report=userAuntVO.getJazUserAuntDO().getReport();
       String noCrime=userAuntVO.getJazUserAuntDO().getNoCrime();

        model.addAttribute("myData", userAuntVO);
        model.addAttribute("experiences",experience==null?null:JSONObject.parse(experience));
        model.addAttribute("lifePhotos",null==lifePhoto||lifePhoto==""?new String[0]:lifePhoto.split(","));
        model.addAttribute("certificates",null==certificate||certificate==""?new String[0]:certificate.split(","));
        model.addAttribute("reports",null==report||report==""?new String[0]:report.split(","));
        model.addAttribute("noCrimes",null==noCrime||noCrime==""?new String[0] :noCrime.split(","));
        model.addAttribute("type", "infor");
        model.addAttribute("userId", id);
        model.addAttribute("inforType", inforType);
        model.addAttribute("aduitEdit", "");
        return "jzweb/jazUserAunt/add";
//        return "jzweb/jazUserAunt/info";
    }


    @ApiOperation(value = "月嫂列表页面入口", notes = "月嫂列表页面入口")
    @ApiImplicitParams({
    })
    @GetMapping()
    @RequiresPermissions("jzweb:jazUserAunt:jazUserAunt")
    public String JazUserAunt(){
        return "jzweb/jazUserAunt/jazUserAunt";
    }


    @ApiOperation(value = "新增月嫂页面入口", notes = "新增月嫂页面入口")
    @ApiImplicitParams({
    })
    @GetMapping("/add")
    @RequiresPermissions("jzweb:jazUserAunt:add")
    public String add(Model model){
        model.addAttribute("type", "add");
        return "jzweb/jazUserAunt/add";
    }


    @ApiOperation(value = "修改月嫂页面入口", notes = "修改月嫂页面入口")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping(value = "/edit/{id}/{sysUserid}/{aduitEdit}")
    public String edit(@PathVariable("id") Long id,@PathVariable("sysUserid") Long sysUserid,@PathVariable("aduitEdit") String aduitEdit, Model model){
        //详情
        UserAuntVO userAuntVO=jazUserAuntService.getUserAuntById(id);
        //处理
        String experience=userAuntVO.getJazUserAuntDO().getExperience();
        String lifePhoto=userAuntVO.getJazUserAuntDO().getLifePhoto();
        String certificate=userAuntVO.getJazUserAuntDO().getCertificate();
        String report=userAuntVO.getJazUserAuntDO().getReport();
        String noCrime=userAuntVO.getJazUserAuntDO().getNoCrime();

        model.addAttribute("myData", userAuntVO);
        model.addAttribute("experiences",experience==null?null:JSONObject.parse(experience));
        model.addAttribute("lifePhotos",null==lifePhoto||lifePhoto==""?new String[0]:lifePhoto.split(","));
        model.addAttribute("certificates",null==certificate||certificate==""?new String[0]:certificate.split(","));
        model.addAttribute("reports",null==report||report==""?new String[0]:report.split(","));
        model.addAttribute("noCrimes",null==noCrime||noCrime==""?new String[0] :noCrime.split(","));
        model.addAttribute("type", "edit");
        model.addAttribute("userId", id);
        model.addAttribute("sysUserid", sysUserid);


        model.addAttribute("aduitEdit", aduitEdit);
        return "jzweb/jazUserAunt/add";
//       return "jzweb/jazUserAunt/edit";

    }
}
